package com.cloudpivot.project.crmprojectnew.service.impl;

import com.cloudpivot.project.crmprojectnew.configuration.CrmConfig;
import com.cloudpivot.project.crmprojectnew.pojo.newoffice.NewOffice;
import com.cloudpivot.project.crmprojectnew.pojo.newoffice.NewOfficeList;
import com.cloudpivot.project.crmprojectnew.service.HttpAPIServiceForCrm;
import com.cloudpivot.project.crmprojectnew.service.NewOfficeService;
import com.cloudpivot.project.crmprojectnew.service.SystemusersService;
import com.cloudpivot.project.crmprojectnew.util.CrmNormalUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class NewOfficeServiceImpl implements NewOfficeService {

    @Autowired
    private CrmConfig crmConfig;

    @Autowired
    private SystemusersService systemusersService;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private HttpAPIServiceForCrm httpAPIServiceForCrm;

    private static final String APISTR = "new_offices";
    private static final String ENTITYSTR = "new_office";

    /**
     * 获取科室信息
     *
     * @param guid
     * @return
     */
    @Override
    public NewOffice getNewOffice(String guid) {
        if ("".equals(guid) || guid == null) {
            return null;
        } else {
            String requestUrl = String.format("%s%s(%s)", crmConfig.getCrmurl(), APISTR, guid);
            try {
                String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl);
                NewOffice newOffice = objectMapper.readValue(responseStr, NewOffice.class);

                return newOffice;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 获取科室信息列表
     *
     * @param userid
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    public NewOfficeList getNewOffices(String userid, int pageIndex, int pageSize) {
        if ("".equals(userid) || userid == null) {
            return null;
        } else {
            NewOfficeList newOfficeList = new NewOfficeList();

            try {

                String requestUrl = CrmNormalUtil.urlEcode(String.format("%s%s?%s", crmConfig.getCrmurl(), APISTR, CrmNormalUtil.getFetchPage(pageIndex, pageSize, ENTITYSTR)));

                String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl, userid);
                newOfficeList = objectMapper.readValue(responseStr, NewOfficeList.class);

                return newOfficeList;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 获取科室信息列表--单一记录
     *
     * @param userid
     * @param guid
     * @return
     */
    @Override
    public NewOfficeList getNewOffices(String userid, String guid) {
        if ("".equals(userid) || userid == null) {
            return null;
        } else {
            try {
                NewOfficeList newOfficeList = new NewOfficeList();
                List<NewOffice> tmpList = new ArrayList<>();
                String requestUrl = String.format("%s%s(%s)", crmConfig.getCrmurl(), APISTR, guid);
                String responseStr = httpAPIServiceForCrm.doGetCrm(requestUrl, userid);
                NewOffice doEntity = objectMapper.readValue(responseStr, NewOffice.class);
                tmpList.add(doEntity);

                newOfficeList.setNewOffices(tmpList);
                newOfficeList.setCount(tmpList.size());
                return newOfficeList;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }

    }

    /**
     * 新增科室信息
     *
     * @param newOffice
     */
    @Override
    public NewOffice addNewOffice(NewOffice newOffice) {
        String userGuid = "";
        if (newOffice == null) {
            return null;
        } else {
            try {
                String requestUrl = String.format("%s%s", crmConfig.getCrmurl(), APISTR);
                String responseStr = httpAPIServiceForCrm.doPostCrmSingle(requestUrl, objectMapper.writeValueAsString(newOffice), userGuid);
                NewOffice doEntity = objectMapper.readValue(responseStr, NewOffice.class);
                return doEntity;
            } catch (Exception ex) {
                ex.printStackTrace();
                return null;
            }
        }
    }

    /**
     * 删除科室信息
     *
     * @param guid
     */
    @Override
    public void delNewOffice(String guid) {
        if (guid != null || !guid.equals("")) {
            String requestUrl = String.format("%s%s(%s)", crmConfig.getCrmurl(), APISTR, guid);
            log.info("请求删除url:{}", requestUrl);
            try {
                httpAPIServiceForCrm.doDeleteCrm(requestUrl);
                log.info("删除记录成功");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * 编辑科室信息
     *
     * @param newOffice
     */
    @Override
    public void editNewOffice(NewOffice newOffice) {

    }
}
